﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// UserAjax 的摘要说明
    /// </summary>
    public class UserAjax : IHttpHandler
    {
        UserController Controller = new UserController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetListIsRebort":
                        GetListIsRebort(context);
                        break;
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "EditUserName":
                        EditUserName(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void EditUserName(HttpContext context)
        {
            int userInfoID = Convert.ToInt32(context.Request["userInfoID"]);
            string userName = context.Request["userName"];
            //1:添加成功 0：用户名已存在
            context.Response.Write(Controller.EditUserName(userInfoID, userName));
        }

        private void GetListIsRebort(HttpContext context)
        {
            List<UserShow> list = Controller.GetRobotList();
            string json = JsonHelper.ListToJson<UserShow>(list);
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void GetList(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);
            string IMSI = context.Request["IMSISELE"] ?? "";
            string sysAccount = context.Request["sysAccount"] ?? "";
            string userAccount = context.Request["userAccount"] ?? "";
            int count = 0;
            List<UserShow> list = Controller.GetList(IMSI, sysAccount, userAccount, pageIndex, pageSize, out count);
            string json = JsonHelper.ListToJson<UserShow>(list, count.ToString());
            context.Response.Write(json);
        }

        private void Add(HttpContext context)
        {
            int communicationID = Convert.ToInt32(context.Request["CommunicationID"]);
            bool isRobot = Convert.ToBoolean(context.Request["IsRobot"]);
            int state = Convert.ToInt32(context.Request["State"]);
            DateTime registerTime = DateTime.Now;
            int userInfoID = Convert.ToInt32(context.Request["UserInfoID"]);
            int pLSettingID = Convert.ToInt32(context.Request["PLSettingID"]);
            string iMSI = context.Request["IMSI"];
            string iMEI = context.Request["IMEI"];
            string number = context.Request["Number"];
            string area = context.Request["Area"];
            string plmn = context.Request["Plmn"];
            string mac = context.Request["Mac"];
            User model = new User()
            {
                CommunicationID = communicationID,
                IsRobot = isRobot,
                State = state,
                RegisterTime = registerTime,
                IMSI = iMSI,
                IMEI = iMEI,
                Number = number,
                Area = area,
                Plmn = plmn,
                Mac = mac
            };
            int id = Controller.Add(model);
            context.Response.Write(id);
        }
        private void Delete(HttpContext context)
        {
            int id = Int32.Parse(context.Request["name"]);
            context.Response.Write(Controller.Delete(id));
        }
        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"]);
            int communicationID = Convert.ToInt32(context.Request["CommunicationID"]);
            int state = Convert.ToInt32(context.Request["State"]);
            int userInfoID = Convert.ToInt32(context.Request["UserInfoID"]);
            int pLSettingID = Convert.ToInt32(context.Request["PLSettingID"]);
            string iMSI = context.Request["IMSI"];
            string iMEI = context.Request["IMEI"];
            string number = context.Request["Number"];
            string area = context.Request["Area"];
            string plmn = context.Request["Plmn"];
            string mac = context.Request["Mac"];

            User model = new User()
            {
                ID = id,
                State = state,
            };
            context.Response.Write(Controller.Update(model));
        }
    }
}